<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/17
 * Time: 15:34
 */

namespace app\admin\validate;


use app\admin\model\Account;
use app\admin\model\Admin;
use think\Validate;

class loginValidate extends Validate
{
    protected $rule = [
        'username' => 'require|checkName',
        'pwd' => 'require',
        'captcha|验证码'=>'require|captcha'
    ];

    protected $message  =   [
        'username' => '账号或者密码错误',
        'pwd' => '账号或者密码错误',
        'captcha' => '验证码错误',
    ];

    protected function checkName($value, $rule='', $data='', $field='')
    {
        $model = new Admin();
        $username = $model->where('username','=',$value)
            ->find();
        if(!$username){
            return '账号不存在';
        }
        $arr = $username->toArray();
        if($arr['status'] == 0){
            return '账号没有启用';
        }
        $pwd = $arr['password'];
        $md5_pwd = md5($data['pwd']);
        if(!($pwd == $md5_pwd)){
            return '密码错误';
        }
        if($arr['scope'] < '32'){
            return '权限不够';
        }
        return true;
    }
}